package _case

import "fmt"

//泛型类型

type user struct {
	ID   int64
	Name string
	Age  uint8
}

type address struct {
	ID       int
	Province string
	City     string
}

// 由集合转列表(切片)
func mapTolist[K comparable, T any](mp map[K]T) []T {
	list := make([]T, len(mp))
	var i int
	for _, date := range mp {
		fmt.Println("列表下标为:", i)
		list[i] = date
		i++
	}
	return list
}

func myPrintLn[T any](ch chan T) {
	for data := range ch {
		fmt.Println(data)
	}
}

func TTypeCase() {
	userMap := make(map[int64]user, 0)
	userMap[1] = user{ID: 1, Name: "wangxd", Age: 11}
	userMap[2] = user{ID: 2, Name: "wangxd2", Age: 22}
	userList := mapTolist[int64, user](userMap)

	ch := make(chan user)

	go myPrintLn(ch)
	for _, u := range userList {
		ch <- u
	}

	addressMap := make(map[int64]address, 0)
	addressMap[1] = address{ID: 1, Province: "湖南", City: "长沙"}
	addressMap[2] = address{ID: 2, Province: "湖2", City: "长2"}
	addrList := mapTolist[int64, address](addressMap)

	ch1 := make(chan address) // make一个通道

	go myPrintLn(ch1)
	for _, u1 := range addrList {
		ch1 <- u1
	}
}

type List[T any] []T

// 泛型集合的定义
type MapT[k comparable, v any] map[k]v

// 泛型通道的定义
type Chan[T any] chan T

func TTypeCase1() {
	userMap := make(MapT[int64, user], 0)
	userMap[1] = user{ID: 1, Name: "wangxd", Age: 11}
	userMap[2] = user{ID: 2, Name: "wangxd2", Age: 22}
	var userList List[user]
	userList = mapTolist[int64, user](userMap)

	ch := make(Chan[user])

	go myPrintLn(ch)
	for _, u := range userList {
		ch <- u
	}

	addressMap := make(MapT[int64, address], 0)
	addressMap[1] = address{ID: 1, Province: "湖南", City: "长沙"}
	addressMap[2] = address{ID: 2, Province: "湖2", City: "长2"}
	var addrList List[address]
	addrList = mapTolist[int64, address](addressMap)

	ch1 := make(Chan[address])

	go myPrintLn(ch1)
	for _, u1 := range addrList {
		ch1 <- u1
	}
}
